// void, Obj This

Hero this;
Unit u;
Item item;
int j, p, power, a;
str favourite_unit;
bool one, one_zero, one_glory, one_notifi, switch;

Sleep(rand(50));

this = This.AsHero;

while(!.IsValid) Sleep(10000);       

while(.player == 15) Sleep(10000);

if(.IsValid){
	if(.name == ""){
	    .SetName("");       
	    .SetName("Hero"+.player+.race+rand(99)+rand(99));
	}
}
        
one_zero = true;
one = true;
one_glory = true;
one_notifi = true;
switch = true;

while(.IsValid){ 
	if(!.IsAlive)
		break;
	if(!.IsHeirOf("TaxCollector")){
	
	if(one_glory){
		one_glory = false;
		if(.item_count > 0){
			item = .GetItem(1);
			if(item.IsValid){
				.AddItem(item.name); 
				.ExchangeItem(item, "iidle");
			}
		} else {
			.AddItem("iidle");
		}
		
		if(.item_count > 1){
			item = .GetItem(2);
			if(item.IsValid){
				.AddItem(item.name); 
				if(EnvReadString(.player, "Generals School") == "researched"){
					.ExchangeItem(item, "General");
				} else {
					if(.IsHeirOf("CaptainOutpost"))
						.ExchangeItem(item, "Captain");
					else
						.ExchangeItem(item, "Officer");
				}
			}
		} else {
			if(EnvReadString(.player, "Generals School") == "researched"){
				.AddItem("General");
			} else {
				if(.IsHeirOf("CaptainOutpost"))
					.AddItem("Captain");
				else 
					.AddItem("Officer");
			}
		}
	}
	
	if(.IsHeirOf("CaptainOutpost")) {
		Sleep(1000);
			
	} else {
	
		if(one_zero){     
			one_zero = false;
			if(.item_count > 2){
				item = .GetItem(3);
				if(item.IsValid){
					.AddItem(item.name);
					.ExchangeItem(item, "UnknownFavourite");
				}
			} else {
				.AddItem("UnknownFavourite");
			}
		}
	
		if(.item_count > 2){
			item = .GetItem(3);
			if(item.IsValid){
				if(item.name == "UnknownFavourite")
					one = true;
			}
		}
		
		if(.HasArmy && one == true){
			one = false;
			.army.ClearDead;
			u = .army[rand(.army.count)].AsUnit;                                   
			if (.race==Carthage){
				if(!.HasItem("CNoble") && !.HasItem("CNumidianRider")
				&& !.HasItem("CMaceman") && !.HasItem("CBerberAssassin")
				&& !.HasItem("CJavelinThrower") && !.HasItem("CLibyanFootman")
				&& !.HasItem("ICavalry") && !.HasItem("CJavelinThrowerLongJavelin")){              

					if(u.class == "CNoble")
						favourite_unit = "CNoble";
					else if(u.class == "CNumidianRider")
						favourite_unit = "CNumidianRider";
					else if(u.class == "CMaceman")
						favourite_unit = "CMaceman";
					else if(u.class == "CBerberAssassin")
						favourite_unit = "CBerberAssassin";
					else if(u.class == "CJavelinThrower")
						favourite_unit = "CJavelinThrower";
					else if(u.class == "CJavelinThrowerLongJavelin")
						favourite_unit = "CJavelinThrower";
					else if(u.class == "CLibyanFootman")
						favourite_unit = "CLibyanFootman";
						
					if(.IsValid())
					if(favourite_unit != ""){
						if(.item_count > 2){
							item = .GetItem(3);
							if(item.IsValid){
								if(item.name != "UnknownFavourite")
									.AddItem(item.name);
								
								.ExchangeItem(item, favourite_unit); 
							}
						} else {
							.AddItem(favourite_unit);
						}
					} else { one = true; }
				}
						
						
			} else if (.race==Iberia){
				if(!.HasItem("IEliteGuard") && !.HasItem("ISlinger")
				&& !.HasItem("IDefender") && !.HasItem("ICavalry")
				&& !.HasItem("IArcher") && !.HasItem("IMilitiaman")
				&& !.HasItem("GHorseman")){            
						
					if(u.class == "IEliteGuard")
						favourite_unit = "IEliteGuard";
					else if(u.class == "ICavalry")    
						favourite_unit = "ICavalry";
					else if(u.class == "ISlinger")
						favourite_unit = "ISlinger";
					else if(u.class == "IDefender")
						favourite_unit = "IDefender";
					else if(u.class == "IArcher")
						favourite_unit = "IArcher";
					else if(u.class == "IArcherLongBow")
						favourite_unit = "IArcher";
					else if(u.class == "IMilitiaman")
						favourite_unit = "IMilitiaman";
								
					if(.IsValid())
					if(favourite_unit != ""){
						if(.item_count > 2){
							item = .GetItem(3);
							if(item.IsValid){
								if(item.name != "UnknownFavourite")
									.AddItem(item.name);		
								
								.ExchangeItem(item, favourite_unit); 
							}
						} else {
							.AddItem(favourite_unit);
						}
					} else { one = true; }
				}
		
			} else if (.race==Britain){
				if(!.HasItem("BHighlander") && !.HasItem("BJavelineer")
				&& !.HasItem("BBronzeSpearman") && !.HasItem("BShieldBearer")
				&& !.HasItem("BBowman") && !.HasItem("BSwordsman")
				&& !.HasItem("TTeuton")){

					if(u.class == "BHighlander")
						favourite_unit = "BHighlander";
					else if(u.class == "BJavelineer")
						favourite_unit = "BJavelineer";
					else if(u.class == "BBronzeSpearman")
						favourite_unit = "BBronzeSpearman";
					else if(u.class == "BShieldBearer")
						favourite_unit = "BShieldBearer";
					else if(u.class == "BBowman")
						favourite_unit = "BBowman";
					else if(u.class == "BBowmanLongBow")
						favourite_unit = "BBowman";
					else if(u.class == "BSwordsman")
						favourite_unit = "BSwordsman";
								
					if(.IsValid())
					if(favourite_unit != ""){
						if(.item_count > 2){
							item = .GetItem(3);
							if(item.IsValid){
								if(item.name != "UnknownFavourite")
									.AddItem(item.name);	
									
								.ExchangeItem(item, favourite_unit); 
							}
						} else {
							.AddItem(favourite_unit);
						}
					} else { one = true; }
				}
						
			} else if (.race==Germany){
				if(!.HasItem("TMaceman") && !.HasItem("THuntress")
				&& !.HasItem("TAxeman") && !.HasItem("TTeuton")
				&& !.HasItem("TArcher") && !.HasItem("TSwordsman")
				&& !.HasItem("BBronzeSpearman")){
					if(u.class == "TMaceman")
						favourite_unit = "TMaceman";
					else if(u.class == "TTeutonRider" || u.class == "TTeutonArcher")    
						favourite_unit = "TTeuton";
					else if(u.class == "THuntress")
						favourite_unit = "THuntress";
					else if(u.class == "TAxeman")
						favourite_unit = "TAxeman";
					else if(u.class == "TArcher")
						favourite_unit = "TArcher";
					else if(u.class == "TArcherLongBow")
						favourite_unit = "TArcher";
					else if(u.class == "TSwordsman")
						favourite_unit = "TSwordsman";
								
					if(.IsValid())
					if(favourite_unit != ""){
						if(.item_count > 2){
							item = .GetItem(3);
							if(item.IsValid){
								if(item.name != "UnknownFavourite")
									.AddItem(item.name);
									
								.ExchangeItem(item, favourite_unit);
							}
						} else {
							.AddItem(favourite_unit);
						}
					} else { one = true; }
				}
						
						
			} else if (.race==Egypt){
				if(!.HasItem("EGuardian") && !.HasItem("EAnubisWarrior")
				&& !.HasItem("EHorusWarrior") && !.HasItem("EAxetrower")
				&& !.HasItem("EArcher") && !.HasItem("ESwordsman")
				&& !.HasItem("CLibyanFootman") && !.HasItem("ICavalry")){

					if(u.class == "EGuardian")
						favourite_unit = "EGuardian";
					else if(u.class == "EHorusWarrior")    
						favourite_unit = "EHorusWarrior";
					else if(u.class == "EAnubisWarrior")
						favourite_unit = "EAnubisWarrior";
					else if(u.class == "EAxetrower")
						favourite_unit = "EAxetrower";
					else if(u.class == "EArcher")
						favourite_unit = "EArcher";
					else if(u.class == "EArcherLongBow")
						favourite_unit = "EArcher";
					else if(u.class == "ESwordsman")
						favourite_unit = "ESwordsman";
							
					if(.IsValid())
					if(favourite_unit != ""){
						if(.item_count > 2){
							item = .GetItem(3);
							if(item.IsValid){
								if(item.name != "UnknownFavourite")
									.AddItem(item.name);
									
								.ExchangeItem(item, favourite_unit); 
							}
						} else {
							.AddItem(favourite_unit);
						}
					} else { one = true; }
				}
						
						
			} else if (.race==Gaul){
				if(!.HasItem("GWomanWarrior") && !.HasItem("GAxeman")
				&& !.HasItem("GHorseman") && !.HasItem("GSpearman")
				&& !.HasItem("GArcher") && !.HasItem("GSwordsman")
				&& !.HasItem("ICavalry")){

					if(u.class == "GWomanWarrior")
						favourite_unit = "GWomanWarrior";
					else if(u.class == "GHorseman")    
						favourite_unit = "GHorseman";
					else if(u.class == "GAxeman")
						favourite_unit = "GAxeman";
					else if(u.class == "GSpearman")
						favourite_unit = "GSpearman";
					else if(u.class == "GArcher")
						favourite_unit = "GArcher";
					else if(u.class == "GArcherLongBow")
						favourite_unit = "GArcher";
					else if(u.class == "GSwordsman")
						favourite_unit = "GSwordsman";
							
					if(.IsValid())
					if(favourite_unit != ""){
						if(.item_count > 2){
							item = .GetItem(3);
							if(item.IsValid){
								if(item.name != "UnknownFavourite")
									.AddItem(item.name);
									
								.ExchangeItem(item, favourite_unit);
							}
						} else {
							.AddItem(favourite_unit);
						}
					} else { one = true; }
				}
		
			} else if(.race==ImperialRome){
				if(!.HasItem("RPraetorian") && !.HasItem("RPrinciple")
				&& !.HasItem("RVelit") && !.HasItem("RScout")
				&& !.HasItem("RArcher") && !.HasItem("RHastatus")
				&& !.HasItem("ICavalry") && !.HasItem("RGladiator")
				&& !.HasItem("RTribune")){

					if(u.class == "RPraetorian")
						favourite_unit = "RPraetorian";
					else if(u.class == "RScout")    
						favourite_unit = "RScout";
					else if(u.class == "RPrinciple")     
						favourite_unit = "RPrinciple";
					else if(u.class == "RGladiator")
						favourite_unit = "RGladiator";
					else if(u.class == "RVelit")
						favourite_unit = "RVelit";
					else if(u.class == "RArcher")     
						favourite_unit = "RArcher";
					else if(u.class == "RArcherLongBow")     
						favourite_unit = "RArcher";
					else if(u.class == "RHastatus")
						favourite_unit = "RHastatus";
					else if(u.class == "RTribune")
						favourite_unit = "RTribune";
							
					if(.IsValid())
					if(favourite_unit != ""){
						if(.item_count > 2){
							item = .GetItem(3);
							if(item.IsValid){
								if(item.name != "UnknownFavourite")
									.AddItem(item.name);
									
								.ExchangeItem(item, favourite_unit); 
							}
						} else {
							.AddItem(favourite_unit);
						}
					} else { one = true; }
				}
	
			} else if(.race==RepublicanRome){
				if(!.HasItem("RTribune") && !.HasItem("RGladiator")
				&& !.HasItem("RPrinciple") && !.HasItem("RScout")
				&& !.HasItem("RArcher") && !.HasItem("RHastatus")
				&& !.HasItem("ICavalry") && !.HasItem("RPraetorian")
				&& !.HasItem("RVelit")){

					if(u.class == "RTribune")
						favourite_unit = "RTribune";
					else if(u.class == "RScout")    
						favourite_unit = "RScout";
					else if(u.class == "RPrinciple")
						favourite_unit = "RPrinciple";
					else if(u.class == "RGladiator")
						favourite_unit = "RGladiator";
					else if(u.class == "RArcher")
						favourite_unit = "RArcher";
					else if(u.class == "RArcherLongBow")     
						favourite_unit = "RArcher";
					else if(u.class == "RHastatus")
						favourite_unit = "RHastatus";
					else if(u.class == "RPraetorian")     
						favourite_unit = "RPraetorian";
					else if(u.class == "RVelit")
						favourite_unit = "RVelit";
						
					if(.IsValid())
					if(favourite_unit != ""){
						if(.item_count > 2){
							item = .GetItem(3);
							if(item.IsValid){
								if(item.name != "UnknownFavourite")
									.AddItem(item.name);
									
								.ExchangeItem(item, favourite_unit);
							}
						} else {
							.AddItem(favourite_unit);
						}
					} else { one = true; }
				}
			}
		}
	}
	
	.army.ClearDead;
	if(.item_count > 0){
		item = .GetItem(1);
		if(item.IsValid){
			if(.command != "getitems"){
				if(.command == "idle"){
					if(.TimePastLastAttack() < GetConst("HeroTimePastLastAttack") 
						&& .EnemiesInSight()){
						
							a = 0;
							for (j = 0; j<.army.count; j+=1){
								u = .army[j].AsUnit;
								if(u.IsValid){
									if(u.IsMilitary){
										if(u.BestTargetForPos.IsMilitary){
											if(!u.BestTargetForPos.IsHeirOf("RamUnit")){
												if(u.BestTargetForPos.IsAlive){
													Sleep(4);
													a+=1;
												}
											}
										}
									}
								}
							}
							if(a >= 14){
								if(item.IsValid)
								if(item.name != "irage_battle")
									.ExchangeItem(item, "irage_battle");
									
								CreateFeedback("Frenzy2", this, 2000);
								Sleep(10);
								CreateFeedback("Parry", this, 2000);
								
							} else {
								if(item.IsValid)
								if(item.name != "ibattle")
									//if(!.HasItem("ibattle"))
										.ExchangeItem(item, "ibattle");
							}
					} else {
						if(item.name != "iidle")
							.ExchangeItem(item, "iidle");
					}
				} else if(.command == "stand_position"){
					if(.TimePastLastAttack() < GetConst("HeroTimePastLastAttack") 
						&& .EnemiesInSight()){
						
							a = 0;
							for (j = 0; j<.army.count; j+=1){
								u = .army[j].AsUnit;
								if(u.IsValid){
									if(u.IsMilitary){
										if(u.BestTargetForPos.IsMilitary){
											if(!u.BestTargetForPos.IsHeirOf("RamUnit")){
												if(u.BestTargetForPos.IsAlive){
													if(.DistTo(u.BestTargetForPos) < 220){
														
														//if(u.IsRanged) Sleep(70);
														Sleep(4);
														a+=1;
													}
												}
											}
										}
									}
								}
							}
							if(a >= 14){
								if(item.IsValid)
								if(item.name != "irage_battle_stand")
									.ExchangeItem(item, "irage_battle_stand");
									
								CreateFeedback("Frenzy2", this, 2000);
								Sleep(10);
								CreateFeedback("Parry", this, 2000);
							} else {
								if(item.IsValid)
								if(item.name != "ibattle_stand")
									if(!.HasItem("ibattle_stand"))
										.ExchangeItem(item, "ibattle_stand");
							}
					} else {
						if(item.IsValid)
						if(item.name != "istand_position")
							.ExchangeItem(item, "istand_position");
					}
				} else if(.command == "capture"){
					if(item.name != "icapture")
						.ExchangeItem(item, "icapture");
				} else if(.command == "retreat_notai" || .command == "retreat_bld_notai"){
					if(item.name != "iretreat_notai"){
						if(item.use_count != 1)
							.ExchangeItem(item, "iretreat_notai");
					}
				} else if(.command == "explore"){
					if(item.name != "iexplore")
						.ExchangeItem(item, "iexplore");
				} else if(.command == "patrol"){
					if(item.name != "ipatrol")
						.ExchangeItem(item, "ipatrol");
				} else if(.command == "great_charge"){
					if(item.name != "igreat_charge")
						.ExchangeItem(item, "igreat_charge");
				} else if(.TimePastLastAttack() < GetConst("HeroTimePastLastAttack") 
					   && .EnemiesInSight()){
				
					a = 0;
					for (j = 0; j<.army.count; j+=1){
						u = .army[j].AsUnit;
						if(u.IsValid){
							if(u.IsMilitary){
								if(u.BestTargetForPos.IsMilitary){
									if(!u.BestTargetForPos.IsHeirOf("RamUnit")){
										if(u.BestTargetForPos.IsAlive){
											if(.DistTo(u.BestTargetForPos) < 220){
											//if(!u.IsRanged){
												//if(u.IsRanged){
												//if(u.DistTo(u.BestTargetForPos.pos) < 235){ // 235
													//if(Intersect(ObjsInRange(u, "Military", 30), EnemyObjs(.player, "Military")).count > 0){
														//a+=1;
													//}
												//}
												if(u.command != "stand_position"){
													if(u.IsHeirOf("Horse"))
														a+=2;
													else
														a+=1;
												}
											//} 
												Sleep(4);
											}
										}
									}
								}
							}
						}
					}
					//pr(a);
					if(a >= 14){
						if(item.IsValid)
						if(item.name != "irage_battle"){
							.ExchangeItem(item, "irage_battle");
							
						}
						//.army.ClearDead;
						//for (j = 0; j<.army.count; j+=1){
							//if(.army[j].AsUnit.IsValid){
								//CreateFeedback("Frenzy2", .army[j].AsUnit, 2000);
								//CreateFeedback("Parry", .army[j].AsUnit, 2000);
							//}
							//Sleep(10);
						//}
						//CreateFeedback("Exclamation_Mark", this);
						CreateFeedback("Frenzy2", this, 2000);
						Sleep(10);
						CreateFeedback("Parry", this, 2000);
						if( .command == "move" ||
							.command == "enter" ||
							.command == "approach" ||
							.command == "capture" ||
							.command == "giveitem" ||
							.command == "getitems" ||
							.command == "dropitem" ||
							.command == "putitem" ||
							.command == "boardshiphero" ||
							.command == "attack" ){
							.army.ClearDead;
							if(.army.count >= 5){
								if(.command != "engage"){
									Sleep(10+rand(50));
									.SetCommand("engage");
								}
							}
						}
						.army.ClearDead;
						for (j = 0; j<.army.count; j+=1){
							if(.army[j].AsUnit.IsValid)
								if(!.army[j].AsUnit.IsRanged)
									.army[j].AsUnit.PlaySound("data/Sound Entities/VOICEBATTLE.XML");
							Sleep(10);
							if(j >= a) break;
						}
					} else {
						if(item.IsValid)
						if(item.name != "ibattle")
							if(!.HasItem("ibattle"))
								.ExchangeItem(item, "ibattle");
					}
					
				} else {
					if(item.IsValid)
					if(item.name != "imove")
						if(!.HasItem("imove"))
							.ExchangeItem(item, "imove");
				}
			} else {
				if(item.IsValid)
				if(item.name != "imove")
					if(!.HasItem("imove"))
						.ExchangeItem(item, "imove");
			}
		}
	}
	.army.ClearDead;
	p = 0;
	for (j = 0; j<.army.count; j+=1){
		u = .army[j].AsUnit;
		if(u.IsValid){
			if(u.IsHeirOf("Piked")){
				Sleep(1);
				p+=1;
			}
		}
	}

	if(p < 7) p = 7;
	power = p * 6;
	EnvWriteInt(.player, "PikedCount/"+.name, power);
	
	if(.HasItem("irage_battle") || .HasItem("irage_battle_stand")){
		if(one_notifi){
			one_notifi = false;
			if (.race==Carthage || .race==Egypt)
				UserNotification("hero in battle arab", "", .posRH, .player);
			else if(.race==ImperialRome || .race==RepublicanRome)
				UserNotification("hero in battle roman", "", .posRH, .player);
			else if(.race==Gaul || .race==Iberia || .race==Britain || .race==Germany)        
				UserNotification("hero in battle barb", "", .posRH, .player);
		}
	} else {
		one_notifi = true;
	}
	
	if(.race==Britain){
		if(rand(100) < 6){
			if(EnvReadString(.player, "Fights") == "researched"){
				Query qBarbarian;
				ObjList l;
				Unit unit;
				int k, count;

				qBarbarian = Intersect(ObjsInRange(this, "Military", 500), ClassPlayerObjs("Military", 15));
				if (qBarbarian.count > 0){
				
					l = qBarbarian.GetObjList();
					l.ClearDead(); 
					count = l.count;
					
					if (count > 0){
					
						k = rand(count);
						unit = l[k];
						if(unit.IsValid){
							if(unit.player == 15){
								if(!unit.IsHeirOf("Sentry")){
									unit.SetPlayer(.player);
									CreateFeedback("Rage", this);
									CreateFeedback("Rage", unit);
								}
							}
						}
					}
				}
			}
		}
    }
	
	.army.ClearDead;
	if(.formation == "Wings"){
		if(switch){
			switch = false;
			for(j=0; j<.army.count; j+=1){
				u = .army[j];
				if(u.IsValid){
					if(u.IsMilitary)
						if(!u.HasItem("Wings formation")){
							Sleep(10);
							u.AddItem("Wings formation");
						}
				}
			}
		}
	} else {
		if(!switch){
			switch = true;
			for(j=0; j<.army.count; j+=1){
				u = .army[j];
				
				if(u.IsValid){
					if(u.HasItem("Wings formation")){
						Sleep(10);
						u.RemoveItemsOfType("Wings formation");
					}
				}
			}
		}
	}
	
	Sleep(1000);
	
	} else {
		if(.formation != "Loose")
			.SetFormation("Loose");
			
		Sleep(60000*3);
	}
}
Sleep(10000); 
